﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Collections;
using System.ComponentModel;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Text;
using System.Net;
using System.Runtime.InteropServices;
using WebServiceSmart.Router;
using Thrift.Transport;
using Thrift.Protocol;
using Thrift.Route;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WebServiceSmart
{
    public static class CommonTools
    {
        public static string ALLYUNDANZIDUAN = "Biao_Createaccount,Biao_Foraccount,Biao_Companyaccount,Biao_Title,Biao_Id," +
                        "Biao_Number,Biao_Time,Biao_Sendplace,Biao_Fromplace,Biao_Passplace,Biao_Sender,Biao_Senderbank," +
                        "Biao_Sendertelephone,Biao_Fromer,Biao_Fromerphone,Biao_Fromertelephone,Biao_Fromeraddress," +
                        "Biao_Goodsname,Biao_Goodsallcount,Biao_Goodscount,Biao_Goodspack,Biao_Goodstiji,Biao_Goodsweight," +
                        "Biao_Goodspayway,Biao_Goodsservice,Biao_Goodsdaishou,Biao_Goodsxianfucost,Biao_Goodstifucost,Biao_Goodshuifucost," +
                        "Biao_Goodsshouyuecost,Biao_Goodsfayuecost,Biao_Goodsyuncost,Biao_Goodsjiecost," +
                        "Biao_Goodssongcost,Biao_Goodschachecost,Biao_Goodsbaozhuangcost,Biao_Goodstuoyicost,Biao_Goodstuoweicost," +
                        "Biao_Goodszhuanyicost,Biao_Goodszhuanweicost,Biao_Goodsdaibancost,Biao_Goodsduanxincost,Biao_Goodschucangcost," +
                        "Biao_Goodsdaocangcost,Biao_Goodsothercost,Biao_Goodsbaocost,Biao_Goodspeicost,Biao_Goodsallcost,Biao_Goodspaycost,Biao_Goodspeifucost," +
                        "Biao_Goodsothercostshuoming,Biao_Goodsgetaddress,Biao_Goodsfujiashuoming,Biao_Toushuphone,Biao_Worker,Biao_Machine," +
                        "Biao_Status,Biao_Isshou,Biao_Ischai,Biao_Carsendid,Biao_Zhuantype,Biao_Zhuanaccount,Biao_Zhuanname,Biao_Daogangtime,Biao_Qianshou,Biao_Qianshouphone," +
                        "Biao_Qianshouid,Biao_Qianshoutime,Biao_Qianshoudefault,Biao_Wanjietime,Biao_Default";

        public static string ALLDAISHOUZIDUAN = "Biao_Createaccount,Biao_Foraccount,Biao_Id,Biao_Number,Biao_Time," +
                      "Biao_Sendplace,Biao_Fromplace,Biao_Sender,Biao_Senderphone,Biao_Fromer,Biao_Fromerphone,Biao_Goodsname," +
                      "Biao_Goodscount,Biao_Goodspayway,Biao_Goodsservice,Biao_YushouData,Biao_ShishouData," +
                      "Biao_ShouxuData,Biao_ShifuData,Biao_Status,Biao_YishouData,Biao_DakuanData,Biao_DaozhangData," +
                      "Biao_HuanpiaoData,Biao_FukuanData,Biao_FukuanRen,Biao_FukuanPhone,Biao_FukuanShuoming,Biao_Default";


        public static string ALLCARSENDZIDUAN = "Carsend_Sid,Carsend_Plate,Carsend_Createtime,Carsend_Driver,Carsend_Driverphone," +
                "Carsend_Sendaccount,Carsend_Sendplace,Carsend_Passaccount,Carsend_Passplace,Carsend_Arriveaccount,Carsend_Arriveplace," +
                "Carsend_Unloadaccount,Carsend_Unloadplace,Carsend_Linetype,Carsend_Sendtime,Carsend_Arrivetime," +
                "Carsend_Status,Carsend_Yayunren,Carsend_Tracellength,Carsend_Zaizhong,Carsend_Carchang,Carsend_Carkuan," +
                "Carsend_Cargao,Carsend_Jiehuofei,Carsend_Songhuofei,Carsend_Shoukuanfei,Carsend_Guolufei,Carsend_Youfei," +
                "Carsend_Yunfei,Carsend_Otherfei,Carsend_Allfei,Carsend_Biaoids," +
                "Carsend_Biaounids,Carsend_Costway,Carsend_Type,Carsend_Banyunworker,Carsend_Shengyutiji," +
                "Carsend_WaiGuan,Carsend_Worker,Carsend_Default";


        public static string ALLDIAODUYICHANGZIDUAN = "diaodu_time,diaodu_biaoid,diaodu_biaonumber,diaodu_huowuname,diaodu_huowubaozhuang," +
        "diaodu_allcount,diaodu_nowcount,diaodu_ids,diaodu_status,diaodu_chugang,diaodu_chugangaccount," +
        "diaodu_daogang,diaodu_daogangaccount,diaodu_fenbo,diaodu_fenboaccount,diaodu_worker,diaodu_default";



        public static string ALLCAIWUZHANGMUZIDUAN = "Caiwu_Account,Caiwu_AccountName,Caiwu_Type,Caiwu_ShouZhi,Caiwu_Name,Caiwu_CiName1,Caiwu_CiName2,Caiwu_LiuXiang," +
        "Caiwu_Cost,Caiwu_Status,Caiwu_Createtime,Caiwu_Shenhetime,Caiwu_Createtype,Caiwu_Sysflag," +
        "Caiwu_Sysflagbind,Caiwu_Starttime,Caiwu_Endtime,Caiwu_Worker,Caiwu_Shenhe,Caiwu_Biaoids,Caiwu_Default";

        public static string ALLDAISHOURECORDZIDUAN = "Contorl_Account,Contorl_Name,Contorl_Time,Contorl_Count,Contorl_Dakuanren,Contorl_Dakuanrenphone,Contorl_Shoukuanren,Contorl_Shoukuanrenphone," +
"Contorl_Bankname,Contorl_Bankaccount,Contorl_Bank,Contorl_Allcost,Contorl_Allshouxucost,Contorl_Ids," +
"Contorl_Type,Contorl_Status,Contorl_Worker,Contorl_Workerphone,Contorl_Default";








        #region 二进制序列化
        //序列化
        public static byte[] ComThriftTOByte(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TBinaryProtocol bp = new TBinaryProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();
            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        public static ComThriftMsg ByteTOComThrift(byte[] ComByte)
        {
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TBinaryProtocol bp = new TBinaryProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }
        //序列化加密
        public static byte[] ComThriftTOByteAESE(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TBinaryProtocol bp = new TBinaryProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();

            retbyte = WebServiceSmart.Router.AesCodec.AESEncryptByte(retbyte, AesCodec.AES_PUBLIC_KEY);
            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        //解密
        public static ComThriftMsg ByteTOComThriftAESD(byte[] ComByte)
        {
            ComByte = AesCodec.AESDecrypt(ComByte, AesCodec.AES_PUBLIC_KEY);
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TBinaryProtocol bp = new TBinaryProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }
        //序列化加密解密加压缩解压缩
        public static byte[] ComThriftTOByteAESE_Encr(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TBinaryProtocol bp = new TBinaryProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();
            retbyte = ZipCodec.Compress(retbyte);
            retbyte = WebServiceSmart.Router.AesCodec.AESEncryptByte(retbyte, AesCodec.AES_PUBLIC_KEY);
            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        public static ComThriftMsg ByteTOComThriftAESD_Decr(byte[] ComByte)
        {

            ComByte = AesCodec.AESDecrypt(ComByte, AesCodec.AES_PUBLIC_KEY);
            ComByte = ZipCodec.DeCompress(ComByte);
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TBinaryProtocol bp = new TBinaryProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }

        //序列化压缩解压缩
        public static byte[] ComThriftTOByteENCR(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TBinaryProtocol bp = new TBinaryProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();
            retbyte = ZipCodec.Compress(retbyte);
            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        public static ComThriftMsg ByteTOComThriftDECR(byte[] ComByte)
        {
            ComByte = ZipCodec.DeCompress(ComByte);
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TBinaryProtocol bp = new TBinaryProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }
        #endregion


        #region json序列化
        //序列化
        public static byte[] Json_ComThriftTOByte(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TJSONProtocol bp = new TJSONProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();
            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        public static ComThriftMsg Json_ByteTOComThrift(byte[] ComByte)
        {
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TJSONProtocol bp = new TJSONProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }
        //序列化加密
        public static byte[] Json_ComThriftTOByteAESE(ComThriftMsg ComInfo, bool IsJsonAES = true)
        {
            if (IsJsonAES)
            {
                TMemoryBuffer mb = new TMemoryBuffer();
                TJSONProtocol bp = new TJSONProtocol(mb);
                ComInfo.Write(bp);
                byte[] retbyte = mb.GetBuffer();

                retbyte = WebServiceSmart.Router.AesCodec.AESEncryptByte(retbyte, AesCodec.AES_PUBLIC_KEY);
                mb.Dispose();
                bp.Dispose();
                return retbyte;
            }
            else
            {
                return ComThriftTOByteAESE(ComInfo);
            }
        }
        //解密
        public static ComThriftMsg Json_ByteTOComThriftAESD(byte[] ComByte)
        {
            ComByte = AesCodec.AESDecrypt(ComByte, AesCodec.AES_PUBLIC_KEY);
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TJSONProtocol bp = new TJSONProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }
        //解密
        public static ComThriftMsg Json_ByteTOComThriftAESD(byte[] ComByte, ref bool IsJsonAES)
        {
            var bytes = ComByte;
            IsJsonAES = true;
            try
            {
                ComByte = AesCodec.AESDecrypt(ComByte, AesCodec.AES_PUBLIC_KEY);
                TMemoryBuffer mb = new TMemoryBuffer(ComByte);
                TJSONProtocol bp = new TJSONProtocol(mb);

                ComThriftMsg ComInfo = new ComThriftMsg();
                ComInfo.Read(bp);
                mb.Dispose();
                bp.Dispose();
                return ComInfo;
            }
            catch { }
            IsJsonAES = false;
            return ByteTOComThriftAESD(bytes);
        }
        //序列化加密解密加压缩解压缩
        public static byte[] Json_ComThriftTOByteAESE_Encr(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TJSONProtocol bp = new TJSONProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();

            string str = HttpUtility.UrlEncode(retbyte);
            retbyte = Encoding.UTF8.GetBytes(str);
            retbyte = ZipCodec.Compress(retbyte);
            retbyte = WebServiceSmart.Router.AesCodec.AESEncryptByte(retbyte, AesCodec.AES_PUBLIC_KEY);

            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        public static ComThriftMsg Json_ByteTOComThriftAESD_Decr(byte[] ComByte)
        {

            ComByte = AesCodec.AESDecrypt(ComByte, AesCodec.AES_PUBLIC_KEY);

            ComByte = ZipCodec.DeCompress(ComByte);
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TJSONProtocol bp = new TJSONProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }

        //序列化压缩解压缩
        public static byte[] Json_ComThriftTOByteENCR(ComThriftMsg ComInfo)
        {
            TMemoryBuffer mb = new TMemoryBuffer();
            TJSONProtocol bp = new TJSONProtocol(mb);
            ComInfo.Write(bp);
            byte[] retbyte = mb.GetBuffer();
            string str = HttpUtility.UrlEncode(retbyte);
            retbyte = Encoding.UTF8.GetBytes(str);
            retbyte = ZipCodec.Compress(retbyte);
            mb.Dispose();
            bp.Dispose();
            return retbyte;
        }
        public static ComThriftMsg Json_ByteTOComThriftDECR(byte[] ComByte)
        {
            ComByte = ZipCodec.DeCompress(ComByte);
            TMemoryBuffer mb = new TMemoryBuffer(ComByte);
            TJSONProtocol bp = new TJSONProtocol(mb);

            ComThriftMsg ComInfo = new ComThriftMsg();
            ComInfo.Read(bp);
            mb.Dispose();
            bp.Dispose();
            return ComInfo;
        }

        #endregion







    }
}
